﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class admin_alert : System.Web.UI.Page
{
    rUser user;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null) { Response.Redirect("../Default.aspx"); }
       if (!Page.IsPostBack){
        fillalerts();
        ListItem li = new ListItem();
        li.Text = "Select One";
        li.Value = "Select One";
        cb_to.Items.Add(li);
        
        Utility.fillAgent("", 3, 2, cb_to);//processor
        Utility.fillAgent("", 7, 1, cb_to);//qc
        Utility.fillAgent("", 7, 1, cb_to);//processing 
        li = new ListItem();
        li.Text = "LB-LucianaB";
        li.Value = "LB";
        cb_to.Items.Add(li);
        bt_save.Visible = true;
        restricted();
        permiso();

       }
    }

    private void permiso()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            // permiso = long.Parse(user.getPermisoUser().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 20);
            permiso = permi.int64C1;
        }



        if ((permiso & 1) != 1) { Pdetail.Enabled = false; Plistalert.Enabled = false; }
    }

    private void restricted()
    {
        long permiso = 0;
        rUser user = (rUser)(Session["userid"]);
        if (user != null)
        {
            //permiso = long.Parse(user.getRestricted().ToString());
            RE_GenericBean permi = Utility.getPermisobyRuser(user, 20);
            permiso = permi.int64C2;
            if ((permiso & 1) == 1) { Response.Redirect(Request.UrlReferrer.ToString()); }
        }
    }

    private void fillalerts()
    {
        DataSet ds= DB.getAllAlerts();
        gv_alert.DataSource = ds;
        gv_alert.DataBind();
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        rUser user= (rUser)Session["userid"];

        if (user != null) {

            RE_GenericBean ra = new RE_GenericBean();
            ra.strC1 = cb_to.SelectedValue.ToString();
            ra.strC2 = user.getAlias();
            ra.strC3 = tb_msg.Text;
            ra.strC6 = DateTime.Now.ToShortTimeString();
            int resp= DB.setAlert(ra);
            DataSet da = DB.getAllAlerts();
            gv_alert.DataSource = da;
            gv_alert.DataBind();
        }
      

    }

    protected void delete(object sender, EventArgs e)
    {
        int index = 0;

        ImageButton bt = (ImageButton)sender;
      
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;
       
        index = objFila.RowIndex;
        int i = 0;


       
        bt_finish_ModalPopupExtender.Show();

        foreach (GridViewRow ROW in gv_alert.Rows)
        {
            if (i == index)
            {
                int con = ROW.Controls.Count;
                Label lb = (Label)ROW.FindControl("lb_id");

                Session["idalert"] = lb.Text;
              


            }

            i++;
        }
    }

    protected void lnk_msg_ok0_Click(object sender, EventArgs e)
    {
        int id = int.Parse((string)Session["idalert"]);
        DB.deleteAlert(id);
        Session["idalert"] = null;
        bt_finish_ModalPopupExtender.Hide();
        DataSet da = DB.getAllAlerts();
        gv_alert.DataSource = da;
        gv_alert.DataBind();


    }
    protected void lnk_msg_ok_Click(object sender, EventArgs e)
    {
        bt_finish_ModalPopupExtender.Hide();
    }
}